<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from 'echarts'
import resize from './mixins/resize'
import { getOneDateAlarmsStatics } from "@/api/statistics" 

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '500px'
    }
  },
  data() {
    return {
      chart: null,
      myColor: ['#33FFCC', '#33CCFF', '#0096f3', '#00c0e9', '#00e9db', '#34da62', '#d0a00e', '#d0570e', '#eb3600', '#eb2100'],
      yAxisData: [],
      option: {
        grid: {
          left: '8%',
          top: '0%',
          right: '0%',
          bottom: '0%',
          containLabel: true
        },
        xAxis: [
          {
            show: false
          }
        ],
        yAxis: [
          {
            name: 'stationNmae',
            axisTick: 'none',
            axisLine: 'none',
            offset: '15',
            axisLabel: {
              textStyle: {
                color: '#ffffff',
                fontSize: '13'
              }
            },
            data: []
          },
          {
            axisTick: 'none',
            axisLine: 'none',
            axisLabel: {
              textStyle: {
                color: '#ffffff',
                fontSize: '10'
              }
            },
            data: []
          },
          {
            axisLine: {
              lineStyle: {
                color: 'rgba(0,0,0,0)'
              }
            },
            data: []
          }
        ],
        series: [
          {
            name: '条',
            type: 'bar',
            yAxisIndex: 0,
            data: [],
            label: {
              normal: {
                show: true,
                position: 'right',
                offset: [10, 0],
                textStyle: {
                  color: '#ffffff',
                  fontSize: '12'
                }
              }
            },
            barWidth: 12,
            itemStyle: {
              normal: {
                
              }
            },
            z: 2
          },
          {
            name: '白框',
            type: 'bar',
            yAxisIndex: 1,
            barGap: '-100%',
            data: [],
            barWidth: 20,
            itemStyle: {
              normal: {
                color: '#0e2147',
                barBorderRadius: 5
              }
            },
            z: 1
          },
          {
            name: '外框',
            type: 'bar',
            yAxisIndex: 2,
            barGap: '-100%',
            data: [],
            barWidth: 24,
            itemStyle: {
              normal: {
                
              }
            },
            z: 0
          },
          {
            name: '外圆',
            type: 'scatter',
            hoverAnimation: false,
            data: [],
            yAxisIndex: 2,
            symbolSize: 28,
            itemStyle: {
              normal: {
                
              }
            },
            z: 2
          }
        ]
      },
      //stationAlarmStatics: {'十三矿':12, '首山一矿':34, '范庄1234站':90, '八矿':33 ,'A':89, 'B':3,'C':56,'D':1,'E':0,'F':22,'G':0,'H':0}, 
      stationAlarmStatics: {}, 
      xAxisData: []
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption(this.option)
      this.asyncGetOneDateAlarmsStatics()
    },
    
    updateChart() {
      this.yAxisData = [[],[]]
      this.xAxisData = [[],[],[],[]]

      let sortedStationAlarmStatics = []
      Object.keys(this.stationAlarmStatics).forEach(key => {
        let stnName = key
        let alarmCnt = this.stationAlarmStatics[key]
        sortedStationAlarmStatics.push({name: stnName, cnt: alarmCnt})
      })

      sortedStationAlarmStatics.sort((a, b) => b.cnt - a.cnt)

      let range = 100
      if (sortedStationAlarmStatics.length > 0) {
        range = Math.max(sortedStationAlarmStatics[0].cnt + 30, range)
      }

      let cnt = Math.min(sortedStationAlarmStatics.length, 10)
      for (let i = cnt - 1; i >= 0; i--) {
        let stnName = sortedStationAlarmStatics[i].name
        let alarmCnt = sortedStationAlarmStatics[i].cnt
        this.yAxisData[0].push(stnName)
        this.yAxisData[1].push(String(cnt - i))
        this.xAxisData[0].push(alarmCnt)
        this.xAxisData[1].push(range)
        this.xAxisData[2].push(range+1)
        this.xAxisData[3].push(0)
      }

      this.option.yAxis[0].data = this.yAxisData[0]
      this.option.yAxis[1].data = this.yAxisData[1]
      this.option.series[0].data = this.xAxisData[0]
      this.option.series[1].data = this.xAxisData[1]
      this.option.series[2].data = this.xAxisData[2]
      this.option.series[3].data = this.xAxisData[3]

      this.option.series[0].itemStyle.normal = {
        color: params => {
          var num = this.myColor.length
          return this.myColor[params.dataIndex % num]
        }
      }
      this.option.series[2].itemStyle.normal = {
        color: params => {
          var num = this.myColor.length
          return this.myColor[params.dataIndex % num]
        },
        barBorderRadius: 5
      }
      this.option.series[3].itemStyle.normal = {
        color: params => {
          var num = this.myColor.length
          return this.myColor[params.dataIndex % num]
        },
        opacity: 1
      }
      this.chart.setOption(this.option)
    },
    async asyncGetOneDateAlarmsStatics() {
      const res = await getOneDateAlarmsStatics()
      if (res.code === 0) {
        this.stationAlarmStatics = res.data.stationAlarmNumberMap
        this.updateChart()
      }
    }
  }
}
</script>
